import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
export const useLoginForm: any = () => {
  const store = useStore()
  const router = useRouter()
  const onLoginClick = async ({
    name,
    password
  }: {
    name?: string
    password?: string
  }) => {
    try {
      const resLogin = await store.dispatch('fetch_login')
      console.log('resLogin:', resLogin)
      if (resLogin.code === 0) {
        ElMessage.success('登陆成功，即将成功')
        setTimeout(() => {
          router.push('/about')
        }, 2000)
      } else {
        ElMessage.error('错了哦，这是一条错误消息')
      }
    } catch (error: any) {
      ElMessage.error(error)
    }
  }

  return { onLoginClick }
}
